package com.darren.euc.dao.impl;


import java.util.List;

import org.hibernate.CacheMode;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.stereotype.Repository;

import com.darren.euc.dao.HanWangDoorDao;
import com.darren.euc.entity.HanWangDoor;


@Repository
public class HanWangDoorDaoImpl extends BaseDaoImpl<HanWangDoor, Integer> implements HanWangDoorDao {
	
	@SuppressWarnings("unchecked")
	@Override
	public List<HanWangDoor> getAll() {
		Session session = sessionFactory.getCurrentSession();
		Query query  = session.createQuery("from HanWangDoor where enable=true");
		query.setCacheMode(CacheMode.IGNORE);
		query.setCacheable(false);
		List<HanWangDoor> hanWangDoors = query.list();
		return hanWangDoors;
	}
	
	@Override
	public int updateHanWangConnectionStatus(Integer id,Integer connectionStatus) {
		//comment '1: 已连接;2: 未连接'
		Session session = sessionFactory.getCurrentSession();
		session.get(HanWangDoor.class, id);
		Query query  = session.createSQLQuery(" update euc_han_wang set connect_status = ?,last_connect_time=NOW() where id = ? ; ");
		query.setParameter(0, connectionStatus);
		query.setParameter(1, id);		
		return query.executeUpdate();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<HanWangDoor> getMainHanWangDoorBySpaceIdNotEquelsId(Integer spaceId,Integer hanwangId){
		Session session = sessionFactory.getCurrentSession();
		Query query = session.createQuery(" from HanWangDoor where enable = true and main=true and spaceId = :spaceId and id <> :hanwangId ");
		query.setParameter("spaceId", spaceId);
		query.setParameter("hanwangId", hanwangId);
		return query.list();
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<HanWangDoor> getMainHanWangDoorBySpaceId(Integer spaceId){
		Session session = sessionFactory.getCurrentSession();
		Query query = session.createQuery(" from HanWangDoor where enable = true and main=true and spaceId = :spaceId ");
		query.setParameter("spaceId", spaceId);
		return query.list();
	}

	@Override
	public List<HanWangDoor> getOtherHanWangDoorBySpaceId(Integer spaceId){
		Session session = sessionFactory.getCurrentSession();
		Query query = session.createQuery(" from HanWangDoor where enable = true and main= false and spaceId = :spaceId ");
		query.setParameter("spaceId", spaceId);
		return query.list();
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<HanWangDoor> getHanWangDoorByMemberId(Integer memberId){
		Session session = sessionFactory.getCurrentSession();
		String SQL = "SELECT hanwang.*  " +
					"	FROM euc_han_wang as hanwang " +
					"	INNER JOIN euc_room as room on room.id = hanwang.room_id " +
					"	where hanwang.`enable` = 1 and exists(select 1 from euc_member as member where find_in_set(cast(room.id as char), member.room_access) and member.id = :memberId) "+
					"   UNION SELECT hanwang.*  FROM euc_han_wang as hanwang where `enable` = 1 and common = 1 ; ";
		Query query = session.createSQLQuery(SQL).addEntity(HanWangDoor.class);
		query.setParameter("memberId", memberId);
		return query.list();
	}
}